<template>
  <div class="card params-div-pos">
    <div class="title1">
      遥测数据列表
    </div>
   <TmTable :table-data="tableData"></TmTable>
  </div>
</template>

<script setup>
import { onMounted, ref, computed, watch, onBeforeMount, onBeforeUnmount } from 'vue'
import { useWsStore } from '@/stores/ws-module'
import TmTable from './components/TmTable.vue'
import { initTm } from '@/api/wsObj.js'

let tableData = ref([
  {
    seq: '序号',
    name: '名称',
    value: '值',
    description: '描述',
    offset: '偏移',
    bits: '位数',
    datatype: '数据类型',
    process: '处理方式',
    method: '采集方式',
    oribytes: '原始字节数',
    unit: '单位',
    range: '取值范围',
    warning: false
  }
])
let curSeqArr = []

const wsStore = useWsStore()
const tmArr = computed(() => wsStore['tm'])
let wsObj = ref(null)
let intervalId = ref(null)

onBeforeMount(() => {
  wsObj = initTm()
  getPassSeq()
  intervalId = setInterval(() => {
    initTable()
  }, 1000)
})

onBeforeUnmount(() => {
  wsObj?.disconnect()
  clearInterval(intervalId)
})

const initTable = () => {
  tableData.value = pickSeqData(curSeqArr, tmArr.value)
}

const pickSeqData = (seqArr, tmObj) => {
  let resArr = []
  for (const item of seqArr) {
    const content = tmObj[item]
    if(content != null) {
      resArr.push(content)
    }
  }
  return resArr
}

const getPassSeq = () => {
  const curHref = window.location.href
  let params = curHref.split("?seq=")[1]
  if(params !== '') {
    curSeqArr = params.split(",")
  }
}

</script>
